package main

import (
	"fmt"
	//"math"
	"math/rand"
	"time"
)

//作业6 生成一个数组(长度10，随机生成值)，计算数组元素的总和以及平均值，并找出超过平均值的数字
func main() {
	var arr = getArr()
	fmt.Println("数组的和为", Sum(arr))
	fmt.Println("数组的平均值为", GetAverage(arr))
	fmt.Println("数组超出平均值的数字为", GreaterAve(arr))

}
func getArr() []int {
	var arr []int
	for i := 0; i < 10; i++ {
		time.Sleep(time.Duration(10) * time.Millisecond)
		rand.Seed(time.Now().UnixNano())
		//fmt.Println(time.Now().UnixNano())
		arr = append(arr, rand.Intn(100))
	}
	return arr
}

func Sum(arr []int) int {
	var sums = 0
	for i := 0; i < len(arr); i++ {
		sums += arr[i]
	}
	return sums
}

func GetAverage(arr []int) float64 {
	var sums = Sum(arr)
	var ave = float64(sums) / 10

	return ave
}

func GreaterAve(arr []int) []int {
	var garr []int
	var ave = GetAverage(arr)
	for i := 0; i < len(arr); i++ {
		if float64(arr[i]) > ave {
			//fmt.Println("大于")
			garr = append(garr, arr[i])
		}
	}
	return garr
}
